﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using StockTrader.DAL.IDAL;
using StockTrader.DAL.MSSQLHelper;
using StockTrader.BusinessService.DataContract;

namespace StockTrader.DAL.SQLserver
{
    public class Period : IPeriod
    {
/*
        private SqlConnection _internalConnection;
        private SqlTransaction _internalADOTransaction = null;

        public void BeginADOTransaction()
        {
            if (_internalConnection.State != System.Data.ConnectionState.Open)
                _internalConnection.Open();
            _internalADOTransaction = _internalConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
        }
        public void RollBackTransaction()
        {
            _internalADOTransaction.Rollback();
            _internalADOTransaction = null;
        }
        public void CommitADOTransaction()
        {
            _internalADOTransaction.Commit();
            _internalADOTransaction = null;
        }
        public void Open(string connString)
        {
            if (_internalConnection == null)
                _internalConnection = new SqlConnection(connString);
            if (_internalConnection.State != ConnectionState.Open)
                _internalConnection.Open();
        }
        public void Close()
        {
            if (_internalConnection != null && _internalConnection.State != System.Data.ConnectionState.Closed)
                _internalConnection.Close();
        }

        //SQL string
        private const string SQL_PERIOD_INSERT = "INSERT INTO PERIOD(NAME, DESCRIPTION, LANG) VALUES (@NAME, @DESCRIPTION, @LANG); SELECT ID=@@IDENTITY;";
        private const string SQL_PERIOD_GETALL = "SELECT * FROM V_PERIOD";
        private const string SQL_PERIOD_GETBYLANG = "SELECT * FROM V_PERIOD WHERE LANG = @LANG";

        #region Parameters
        private const string PARM_ID = "@ID";
        private const string PARM_NAME = "@NAME";
        private const string PARM_DESCRIPTION = "@DESCRIPTION";
        private const string PARM_LANG = "@LANG";
        #endregion

        //Functions
        #region IPeriod Members


        public int InsertPeriod(PeriodUI item)
        {
            try
            {
                SqlParameter[] parms = new SqlParameter[] {
                    new SqlParameter(PARM_NAME, SqlDbType.NVarChar),
                    new SqlParameter(PARM_DESCRIPTION, SqlDbType.NText),
                    new SqlParameter(PARM_LANG, SqlDbType.Int)};
                parms[0].Value = item.Name;
                parms[1].Value = item.Description;
                parms[2].Value = item.Lang;
                return SQLHelper.ExecuteNonQuery(_internalConnection, _internalADOTransaction, CommandType.Text, SQL_PERIOD_INSERT, parms);
            }
            catch
            {
                throw;
            }
        }

        public List<PeriodUI> GetPeriod()
        {
            try
            {
                List<PeriodUI> staList = new List<PeriodUI>();
                SqlDataReader rdr = SQLHelper.ExecuteReaderNoParm(_internalConnection, _internalADOTransaction, CommandType.Text, SQL_PERIOD_GETALL);
                while (rdr.Read())
                {
                    PeriodUI item = new PeriodUI(Convert.ToInt32(rdr["ID"]), rdr["NAME"].ToString(), rdr["DESCRIPTION"].ToString(), Convert.ToInt32(rdr["LANG"]), rdr["LANGTEXT"].ToString());
                    staList.Add(item);
                }
                rdr.Close();
                return staList;
            }
            catch
            {
                throw;
            }
        }

        public List<PeriodUI> GetPeriodByLang(int lang)
        {
            try
            {
                SqlParameter parm = new SqlParameter(PARM_LANG, SqlDbType.Int);
                parm.Value = lang;
                List<PeriodUI> staList = new List<PeriodUI>();
                SqlDataReader rdr = SQLHelper.ExecuteReaderSingleParm(_internalConnection, _internalADOTransaction, CommandType.Text, SQL_PERIOD_GETBYLANG, parm);
                while (rdr.Read())
                {
                    PeriodUI item = new PeriodUI(Convert.ToInt32(rdr["ID"]), rdr["NAME"].ToString(), rdr["DESCRIPTION"].ToString(), Convert.ToInt32(rdr["LANG"]), rdr["LANGTEXT"].ToString());
                    staList.Add(item);
                }
                rdr.Close();
                return staList;
            }
            catch
            {
                throw;
            }
        }

        #endregion
*/

    }
}
